package com.power.algorithm.structure.linked.duplex;

/**
 * @description:
 * @author: jiangnan
 * @time: 2021/5/19 3:33 下午
 */
public class DuplexList<E> {

    transient Node<E> head;
    transient Node<E> tail;
    private int size;

    public static class Node<E> {
        Node<E> next;
        Node<E> pre;
        E value;

        public Node(E value) {
            this.value = value;
        }

        public Node(Node pre, Node next, E value) {
            this.pre = pre;
            this.next = next;
            this.value = value;
        }
    }

    public DuplexList() {
        // head = tail = new Node<>(null, null, null);
    }

    public Node add(E value) {
        Node newNode = new Node(value);
        //如果为空的链表
        if (size == 0) {
            head = newNode;
        } else {
            Node tmp = head;
            while (tmp.next != null) {
                tmp = tmp.next;
            }
            tmp.next = newNode;
            newNode.pre = tmp;
        }
        size++;
        return newNode;
    }

    public void print() {
        Node tmp = head;
        while (tmp.next != null) {
            System.out.println(tmp.value);
            tmp = tmp.next;
        }
        System.out.println(tmp.value);
    }

    public int size() {
        return size;
    }

    public static void main(String[] args) {
        DuplexList duplexList = new DuplexList();
        duplexList.add(1);
        duplexList.add(1);
        duplexList.print();
    }
}
